using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SanchoJoystick
{
    public partial class Joystick : Form
    {
        private ControlSancho.Robot m_sancho;

        public Joystick()
        {
            InitializeComponent();

            port.DataSource = SerialPort.GetPortNames();
            port.SelectedIndex = SerialPort.GetPortNames().Length - 1;
            bps.SelectedIndex = bps.Items.Count - 1;
            flow.SelectedIndex = 0;

            Connect();
        }

        private void Connect()
        {
            m_sancho = new ControlSancho.Robot((string)port.SelectedItem, Int32.Parse((string)bps.SelectedItem), (Handshake)flow.SelectedIndex);
            m_sancho.Puerto.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
            m_sancho.Puerto.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorReceived);
        }

        private void buttonParar_Click(object sender, EventArgs e)
        {
            m_sancho.SetSpeed(0, 0);
        }

        private void buttonAvanzar_Click(object sender, EventArgs e)
        {
            m_sancho.VelIzq += 10;
            m_sancho.VelDcha += 10;
        }

        private void buttonRetroceder_Click(object sender, EventArgs e)
        {
            m_sancho.VelIzq -= 10;
            m_sancho.VelDcha -= 10;
        }

        private void buttonIzquierda_Click(object sender, EventArgs e)
        {
            m_sancho.VelIzq -= 10;
            m_sancho.VelDcha += 10;
        }

        private void buttonDerecha_Click(object sender, EventArgs e)
        {
            m_sancho.VelIzq += 10;
            m_sancho.VelDcha -= 10;
        }

        private void DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(
                    new SerialDataReceivedEventHandler(DataReceived),
                    new object[] { sender, e });
            }
            else
            {
                int bytesNumber = m_sancho.Puerto.BytesToRead;
                char[] buffer = new char[bytesNumber];
                m_sancho.Puerto.Read(buffer, 0, bytesNumber);

                string salida = new String(buffer);
                salida = salida.Replace("\n\r", "\r\n");
                textBoxSalida.Text += salida;
            }
        }

        private void ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(
                    new SerialErrorReceivedEventHandler(ErrorReceived),
                    new object[] { sender, e });
            }
            else
            {
                textBoxSalida.Text += "Error:" + e.ToString() + "\n";
            }
        }

        private void reconnect_Click(object sender, EventArgs e)
        {
            Connect();
        }
    }
}